﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FMOD;
using TagLib;
using SmallTune.Models;

namespace SmallTune.Controllers
{
    public class PlayerController : BaseController
    {
        private Channel fChannel;
        private Sound fSound;
        private FMOD.System fSystem;
        private FMOD.RESULT FMOD_RESULT = FMOD.RESULT.OK;

        #region DisplayInfos
        private string _displayTitle;

        public string displayTitle
        {
            set
            {
                _displayTitle = value;
                OnPropertyChanged("displayTitle");
            }
            get
            {
                return _displayTitle;
            }
        }

        private string _displayAlbum;

        public string displayAlbum
        {
            get
            {
                return _displayAlbum;
            }
            set
            {
                _displayAlbum = value;
                OnPropertyChanged("displayAlbum");
            }
        }


        private string _displayArtist;

        public string displayArtist
        {
            get
            {
                return _displayArtist;
            }
            set
            {
                _displayArtist = value;
                OnPropertyChanged("displayArtist");
            }
        }

        private string _displayCurrentTime;

        public string displayCurrentTime
        {
            get
            {
                return _displayCurrentTime;
            }
            set
            {
                _displayCurrentTime = value;
                OnPropertyChanged("displayCurrentTime");
            }
        }

        private string _displayTotalTime;

        public string displayTotalTime
        {
            get
            {
                return _displayTotalTime;
            }
            set
            {
                _displayTotalTime = value;
                OnPropertyChanged("displayTotalTime");
            }
        }
        #endregion

        #region ControlbarCommands
        #endregion

        public PlayerController()
        {
            //Init Displays
            displayTitle = "---";
            displayArtist = "---";
            displayAlbum = "---";

            displayCurrentTime = "--:--";
            displayTotalTime = "--:--";
        }

        public void PlaySong()
        {
            FMOD_RESULT = FMOD.Factory.System_Create(ref this.fSystem);
            FMOD_RESULT = fSystem.init(1, FMOD.INITFLAGS.NORMAL, (IntPtr)null);

            //Read tags first
            TagLib.File tagfile = TagLib.File.Create(@"D:\Daniel\Musik\ACDC-Back_In_Black-Remastered-CD-FLAC-2003-PERFECT\01-acdc-hells_bells.flac");

            string title = tagfile.Tag.Title;

            var song = new Song();
            var album = new Album { name = tagfile.Tag.Album };
            var artist = new Artist { name = tagfile.Tag.FirstAlbumArtist };

            song.title = tagfile.Tag.Title;
            song.durationInSeconds = 0;

            tagfile.Dispose();



            fSystem.createSound(@"D:\Daniel\Musik\ACDC-Back_In_Black-Remastered-CD-FLAC-2003-PERFECT\01-acdc-hells_bells.flac", (FMOD.MODE._2D | FMOD.MODE.HARDWARE | FMOD.MODE.ACCURATETIME | FMOD.MODE.CREATESTREAM), ref fSound);

            fSystem.playSound(FMOD.CHANNELINDEX.FREE, fSound, false, ref fChannel);

            uint seconds = 0;
            fSound.getLength(ref seconds, FMOD.TIMEUNIT.MS);
            TimeSpan t = TimeSpan.FromMilliseconds(seconds);

            song.AddArtist(artist);
            album.AddSong(song);
            artist.AddAlbum(album);

            displayTotalTime = string.Format("{0:D2}:{1:D2}",
                                    t.Minutes,
                                    t.Seconds);
            displayTitle = title;
            displayAlbum = song.listOfAlbums[0].name;
            displayArtist = song.listOfArtists[0].name;
        }

        public override void MessageNotification(string message, object args)
        {

        }
    }
}
